<?php
/**
 * Extend the UserManager class to add Zend_Session support
 *
 * @author @@AUTHOR@@
 * @copyright @@AUTHOR@@ @@YEAR@@
 * @version @@VERSION@@
 * @package Extensions
 * @subpackage DbSession
 * @license  @@LICENCE_URL@@ @@LICENCE@@
 *
 * $Id: People.Class.DbSessionUserManager.php 322 2008-10-11 13:03:00Z dinoboff $
 * $HeadURL: http://vanilla-friends.googlecode.com/svn/trunk/DbSession/library/People/People.Class.DbSessionUserManager.php $
 */

if (!defined('IN_VANILLA')) exit();

/**
 * @global Context $Context
 */
global $Context;
include_once $Context->Configuration['LIBRARY_PATH'] . '/People/People.Class.UserManager.php';

/**
 * Make UserManager use $Context->Session instead of $_SESSION
 * @package Extensions
 * @subpackage DbSession
 */
class DbSessionUserManager extends UserManager  {
	/**
	 * Change user's password
	 *
	 * @param User $User
	 * @return boolean
	 */
	function ChangePassword($User) {
		if ($this->Context->Configuration['ALLOW_PASSWORD_CHANGE']) {
			// Ensure that the person performing this action has access to do so
			// Everyone can edit themselves
			if ($this->Context->Session->UserID != $User->UserID
				&& !$this->Context->Session->User->Permission('PERMISSION_EDIT_USERS')
			) {
				$this->Context->WarningCollector->Add(
					$this->Context->GetDefinition('ErrPermissionInsufficient'));
				return 0;
			}

			// Validate inputs
			Validate($this->Context->GetDefinition('NewPasswordLower'), 1, $User->NewPassword, 100, '', $this->Context);
			if ($User->NewPassword != $User->ConfirmPassword) $this->Context->WarningCollector->Add($this->Context->GetDefinition('ErrNewPasswordMatchBad'));

			// Validate old password
			$UserCredentials = $this->GetUserCredentials($User->UserID);
			// Reset verification key
			$UserCredentials->VerificationKey = DefineVerificationKey();
			if (!$this->CheckPassword($UserCredentials, $User->OldPassword, 0)) {
				$this->Context->WarningCollector->Add(
					$this->Context->GetDefinition('ErrOldPasswordBad'));
			} // Set new Password
			else if ($this->Context->WarningCollector->Count() == 0) {
				if ($this->SetNewPassword($User, $User->NewPassword)
					&& $this->Context->Session->UserID == $User->UserID
				) {
					$this->Context->Session->RegenerateId($this->Context);
				}
			}
		}
		return $this->Context->WarningCollector->Iif();
	}

	function AddCategoryBlock($CategoryID) {
		$Success = parent::AddCategoryBlock($CategoryID);
		if ($Success){
			$this->Context->Session->SetUserDetails($this->Context, 1);
		}
		return $Success;
	}

	function AddCommentBlock($CommentID) {
		$Success = parent::AddCommentBlock($CommentID);
		if ($Success){
			$this->Context->Session->SetUserDetails($this->Context, 1);
		}
		return $Success;
	}

	function AddUserBlock($UserID) {
		$Success = parent::AddUserBlock($UserID);
		if ($Success){
			$this->Context->Session->SetUserDetails($this->Context, 1);
		}
		return $Success;
	}

	function AssignRole($UserRoleHistory, $NewUser = '0') {
		$Success = parent::AssignRole($UserRoleHistory, $NewUser);
		if ($Success) {
			AddConfigurationSetting($this->Context, 'DBSESSION_LAST_UPDATE', mktime());
		}
		return $Success;
	}

	function RemoveCategoryBlock($CategoryID) {
		$Success = parent::RemoveCategoryBlock($CategoryID);
		if ($Success){
			$this->Context->Session->SetUserDetails($this->Context, 1);
		}
		return $Success;
	}

	function RemoveCommentBlock($CommentID) {
		$Success = parent::RemoveCommentBlock($CommentID);
		if ($Success){
			$this->Context->Session->SetUserDetails($this->Context, 1);
		}
		return $Success;
	}

	function RemoveUserBlock($UserID) {
		$Success = parent::RemoveUserBlock($UserID);
		if ($Success){
			$this->Context->Session->SetUserDetails($this->Context, 1);
		}
		return $Success;
	}

	function SaveIdentity($User) {
		$Success = parent::SaveIdentity($User);
		if ($Success) {
			 if ($this->Context->Session->UserID === $User->UserID){
			 	$this->Context->Session->SetUserDetails($this->Context, 1);
			 } else {
			 	// reset all session data maybe
			 }
		}
		return $Success;
	}

	function SaveStyle($User) {
		$Success = parent::SaveStyle($User);
		if ($Success
			&& $this->Context->Session->UserID === $User->UserID
		){
			$this->Context->Session->SetUserDetails($this->Context, 1);
		}
		return $Success;
	}

	function SetDefaultFormatType($UserID, $FormatType) {
		return $this->SwitchUserProperty($UserID, 'DefaultFormatType', $FormatType);
	}

	function SwitchUserProperty($UserID, $PropertyName, $Switch) {
		$Success = parent::SwitchUserProperty($UserID, $PropertyName, $Switch);
		if ($Success
			&& $this->Context->Session->UserID === $UserID
		){
			$this->Context->Session->SetUserDetails($this->Context, 1);
		}
		return $Success;
	}

	function SaveUserPreferences($User) {
		$Success = parent::SaveUserPreferences($User);
		if ($Success
			&& $this->Context->Session->UserID === $User->UserID
		){
			$this->Context->Session->SetUserDetails($this->Context, 1);
		}
		return $Success;
	}

}
